package com.zhz.lib.project.service.custom;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhz.lib.project.ibatis.dao.custom.CustomDAO;
import com.zhz.lib.project.ibatis.domain.custom.Custom;
import com.zhz.lib.project.vo.custom.CustomVO;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-2 下午08:40:58
 * 
 */
@Service
public class CustomService {
    private static Logger logger = Logger.getLogger(CustomService.class);
    @Autowired
    private CustomDAO     customDAO;

    /**
     * 获取顶级模块
     * 
     * @return
     */
    public List<CustomVO> getTopCustom() {
        return this.getSubCustom("0");
    }

    /**
     * 获取某个模块的子模块，查询条件《1-未删除、2-审核通过、3-》
     * 
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<CustomVO> getSubCustom(String id) {
        // List<Custom> topList = baseDao.find("Custom", "sortType", id);
        List<Custom> topList = customDAO.getCustomBySortType(id);
        List<CustomVO> topVOList = new ArrayList<CustomVO>();
        if (topList != null && topList.size() > 0) {
            for (Custom custom : topList) {
                topVOList.add(this.Custom2VO(custom));
                // 设置该模块的子模块
            }
        }

        return topVOList;
    }

    /**
     * 获取某个模块的子模块中的第一个
     * 
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public CustomVO getFirstOfSubCustom(String id) {

        List<Custom> list = customDAO.getCustomBySortType(id);
        if (list != null && list.size() > 0) {
            return this.Custom2VO(list.get(0));
        }
        return null;
    }

    /**
     * 获取某个模块
     * 
     * @param id
     * @return
     */
    public CustomVO getCustomByCustomID(String id) {

        CustomVO customVO = this.Custom2VO(customDAO.getCustomById(id));
        if (customVO != null)
            customVO.setSubCustomVOList(this.getSubCustom(customDAO.getCustomById(id).getCustomId()
                                                          + ""));
        return customVO;

    }

    /**
     * 将Custom转换为CustomVO，不获取其子模块
     * 
     * @param custom
     * @return
     */
    private CustomVO Custom2VO(Custom custom) {
        CustomVO customVO = new CustomVO();
        try {
            BeanUtils.copyProperties(customVO, custom);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("从custom复制到customVO时发生异常", e);
        }
        return customVO;
    }

    /**
     * 点击次数增加1
     * 
     * @param hotlinkVO
     * @return
     */
    public void customHitPlusOne(int id) {
        customDAO.customHitPlusOne(id + "");
    }
}
